<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
      ①定义dp数组和下标含义：
      dp[i][1]表示第i天 持有股票的最大价值
      dp[i][2]表示第i天 不持有股票的最大价值
      */
      var maxProfit = function (prices) {
        let dp = new Array(prices.length + 1).fill().map(() => new Array(3).fill(0))
        dp[0][1] = -prices[0]
        for (let i = 1; i <= prices.length; i++) {
          dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][2] - prices[i - 1])
          dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i - 1])
        }
        return dp[prices.length][2]
      }
    </script>
  </body>
</html>
